﻿using System;
using System.Runtime.InteropServices;
using Alvin.Window.Utils.TouchView.Keyboard;

namespace Alvin.Window.Utils
{
    public static class KeyboardUtisl
    {

        private static NumberKeyboradDialog keyboradDilog = null;
        private static double left = 0;
        private static double top = 0;

        public static NumberKeyboradDialog KeyboradDialog
        {
            get
            {
                if (keyboradDilog == null)
                {
                    keyboradDilog = new NumberKeyboradDialog();
                }
                return keyboradDilog;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
         static KeyboardUtisl()
        {

        }

        public static void KeyboardTool(System.Windows.Window window)
        {
            var location = window.WindowStartupLocation;
            KeyboradDialog.WindowStartupLocation = location;
            
            KeyboardShow();
        }

        /// <summary>
        /// 显示键盘
        /// </summary>
        public static void KeyboardShow()
         {
             if (KeyboradDialog.IsActive)
             {
                 keyboradDilog.ShowActivated = false ;
             }
             else
             {
                 //keyboradDilog.ShowActivated = true;
                 //keyboradDilog.Focusable = false;
                 //keyboradDilog.Show();

                 //DialogHelper.SetWindowOwner(KeyboradDialog);
                 //"存储当前的活动窗口，便于激活。";
                 //Window topWin = DialogHelper.GetTopWindow();

                 //"显示弹出窗口。";
                 KeyboradDialog.Show();
                 //// "设置窗口Z顺序。";
                 //DialogHelper.SetWindowZOrder(keyboradDilog);
                 //keyboradDilog.ShowActivated = false;

                 //"激活当前活动窗口。";
                 //topWin.Activate();
             }
         }

        /// <summary>
        /// 关闭键盘
        /// </summary>
       public static void KeyboradClose()
        {
            if (keyboradDilog.IsVisible)
            {
                keyboradDilog.Hide();
            }
        }


    }

    internal class KeyboardSend
    {
        private const uint KEYEVENTF_EXTENDEDKEY = 0x1;
        private const uint KEYEVENTF_KEYUP = 0x2;



        [DllImport("user32.dll")]
        private static extern short GetKeyState(int nVirtKey);

        [DllImport("user32.dll")]
        private static extern void keybd_event(
            byte bVk,
            byte bScan,
            uint dwFlags,
            uint dwExtraInfo
            );

        public enum VirtualKeys : byte
        {
            VK_NUMLOCK = 0x90,      //数字锁定键 
            VK_SCROLL = 0x91,       //滚动锁定 
            VK_CAPITAL = 0x14,      //大小写锁定 
            VK_BACK = 0x08,         //BACKSPACE key
            VK_OEM_CLEAR = 0xFE,    //Clear key
            VK_CLEAR = 0x0C,        //CLEAR key
            VK_RETURN = 0x0D,       //ENTER key
            VK_ESCAPE = 0x1B,       //ESC key
            VK_SHIFT = 0x10,        //SHIFT key
            VK_POINT=0x6E,         //Point Key
            VK_SEPARATOR = 0x6C,      //Subtract key
            VK_ADD = 0x6B,            //Add key
            VK_OEM_1=0xBA,          //; KEY
            VK_OEM_2,
            VK_OEM_COMMA = 0xDE,          //,KEY

            key_0 = 0x60,
            key_1,
            key_2,
            key_3,
            key_4,
            key_5,
            key_6,
            key_7,
            key_8,
            key_9,
            A = 0x41,
            B,
            C,
            D,
            E,
            F,
            G,
            H,
            I,
            J,
            K,
            L,
            M,
            N,
            O,
            P,
            Q,
            R,
            S,
            T,
            U,
            V,
            W,
            X,
            Y,
            Z,
            Key_0=0x30,
            Key_1,
            Key_2,
            Key_3,
            Key_4,
            Key_5,
            Key_6,
            Key_7,
            Key_8,
            Key_9

        }

        /// <summary>
        /// 获取按键状态
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static bool GetState(VirtualKeys Key)
        {
            return (GetKeyState((int)Key) == 1);
        }

        /// <summary>
        /// 模拟键盘发送一个键
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="State"></param>
        public static void SetState(VirtualKeys Key, bool State)
        {
            if (State != GetState(Key))
            {
                keybd_event(
                    (byte)Key,
                    0x45,
                    KEYEVENTF_EXTENDEDKEY | 0,
                    0
                    );
                keybd_event(
                    (byte)Key,
                    0x45,
                    KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                    0
                    );
            }
        }

        /// <summary>
        /// 模拟一个键按下
        /// </summary>
        /// <param name="key"></param>
        public static void PushKeyDown(VirtualKeys key)
        {
            keybd_event(
                (byte)key,
                0x45,
                KEYEVENTF_EXTENDEDKEY | 0,
                0
                );
        }

        /// <summary>
        /// 模拟一个键弹起
        /// </summary>
        /// <param name="key"></param>
        public static void PushKeyUp(VirtualKeys key)
        {
            keybd_event(
                (byte)key,
                0x45,
                KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                0
                );
        }

        /// <summary>
        /// 模拟一个按键
        /// </summary>
        /// <param name="name"></param>
        public static void PushKey(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return;
            }
            VirtualKeys number;
            if (Enum.TryParse(name, out number))
            {
                if (Enum.IsDefined(typeof(VirtualKeys), number))
                {
                    SetState(number, !GetState(number));
                }
            }
        }

        /// <summary>
        /// 模拟组合按键
        /// </summary>
        public static void PushDoubleKey(VirtualKeys key)
        {
            SetState(key, !GetState(key));
            SetState(key, !GetState(key));
        }

        /// <summary>
        /// 模拟组合按键
        /// </summary>
        public static void PushTeamKeys(string teamName1, string teamName2)
        {
            bool key1Down = false;
            bool key2Down = false;
            VirtualKeys teamKey;
            if (Enum.TryParse(teamName1, out teamKey))
            {
                if (Enum.IsDefined(typeof(VirtualKeys), teamKey))
                {
                    PushKeyDown(teamKey);
                    key1Down = true;
                }
            }
            VirtualKeys teamKey2;
            if (Enum.TryParse(teamName2, out teamKey2))
            {
                if (Enum.IsDefined(typeof(VirtualKeys), teamKey2))
                {
                    PushKeyDown(teamKey2);
                    key2Down = true;
                }
            }
            if (key1Down)
            {
                PushKeyUp(teamKey);
            }
            if (key2Down)
            {
                PushKeyUp(teamKey2);
            }
        }
    }
}
